﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private async Task<string> DownloadHtmlAsync(string url)
        {
            using (HttpClient client = new HttpClient())
            {
                // 设置User-Agent避免被服务器拒绝
                client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");
                return await client.GetStringAsync(url);
            }
        }
        private List<string> ExtractPhoneNumbers(string input)
        {
            string pattern = @"(\+?86[-\s]?)?1[3-9]\d{2}[-\s]?\d{4}[-\s]?\d{4}";
            Regex regex = new Regex(pattern);
            return regex.Matches(input)
                       .Cast<Match>()
                       .Select(m => m.Value.Replace(" ", "").Replace("-", "")) // 去除分隔符
                       .Distinct()
                       .ToList();
        }

        private List<string> ExtractEmails(string input)
        {
            string pattern = @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b";
            Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
            return regex.Matches(input)
                       .Cast<Match>()
                       .Select(m => m.Value)
                       .Distinct()
                       .ToList();
        }
        private async void btnFetch_Click(object sender, RoutedEventArgs e)
        {
            string url = txtUrl.Text.Trim();
            if (string.IsNullOrWhiteSpace(url))
            {
                MessageBox.Show("请输入有效的URL");
                return;
            }

            try
            {
                string html = await DownloadHtmlAsync(url);

                // 清空旧数据
                lstPhones.Items.Clear();
                lstEmails.Items.Clear();

                // 提取信息
                var phones = ExtractPhoneNumbers(html);
                var emails = ExtractEmails(html);

                // 显示结果
                lstPhones.Items.Add(phones.ToArray());
                lstEmails.Items.Add(emails.ToArray());
            }
            catch (UriFormatException)
            {
                MessageBox.Show("URL格式不正确");
            }
            catch (HttpRequestException ex)
            {
                MessageBox.Show($"网络错误：{ex.Message}");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"发生错误：{ex.Message}");
            }
        }

        private void lstPhones_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
